package org.cx.comparator.spring;

import org.cx.comparator.ComparatorConfiguration;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;


public class ComparatorFactoryBean<T> implements FactoryBean<T>, InitializingBean {

    private Class<T> mapperInterface;
    private ComparatorConfiguration comparatorConfiguration;


    public ComparatorFactoryBean() {
    }

    public ComparatorConfiguration getComparatorConfiguration() {
        return comparatorConfiguration;
    }

    public void setComparatorConfiguration(ComparatorConfiguration comparatorConfiguration) {
        this.comparatorConfiguration = comparatorConfiguration;
    }

    public ComparatorFactoryBean(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @Override
    public T getObject() throws Exception {
        return comparatorConfiguration.getComparatorRegistry().getComparator(mapperInterface);
    }

    @Override
    public Class<?> getObjectType() {
        return this.mapperInterface;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        comparatorConfiguration.getComparatorRegistry().addComparator(mapperInterface);
    }
}
